package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.exception.CustomException;
import com.ruoyi.system.domain.SysTempMenu;
import com.ruoyi.system.service.ISysTempMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.system.mapper.SysTemplateMapper;
import com.ruoyi.system.domain.SysTemplate;
import com.ruoyi.system.service.ISysTemplateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.lang.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * 租户权限Service业务层处理
 *
 * @author ruoyi
 * @date 2021-02-02
 */
@Service
public class SysTemplateServiceImpl extends ServiceImpl<SysTemplateMapper, SysTemplate> implements ISysTemplateService {
    @Autowired
    private ISysTempMenuService tempMenuService;
    @Override
    public List<SysTemplate> queryList(SysTemplate sysTemplate) {
        LambdaQueryWrapper<SysTemplate> lqw = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(sysTemplate.getName())) {
            lqw.like(SysTemplate::getName, sysTemplate.getName());
        }
        return this.list(lqw);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertSysTemplate(SysTemplate sysTemplate) {
        save(sysTemplate);
        List<Integer> menuIds = sysTemplate.getMenuIds();
        if(CollectionUtils.isEmpty(menuIds)){
            throw new CustomException("菜单id不能为空");
        }
        resetSysTempMenus(sysTemplate, menuIds);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateSysTemplate(SysTemplate sysTemplate) {
        List<Integer> menuIds = sysTemplate.getMenuIds();
        if(CollectionUtils.isEmpty(menuIds)){
            throw new CustomException("菜单id不能为空");
        }
        resetSysTempMenus(sysTemplate,sysTemplate.getMenuIds());
        updateById(sysTemplate);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void removeSysTemplateByIds(List<String> asList) {
        tempMenuService.remove(new QueryWrapper<SysTempMenu>().lambda().in(SysTempMenu::getTempId,asList));
        removeByIds(asList);
    }

    private void resetSysTempMenus(SysTemplate sysTemplate, List<Integer> menuIds) {
        //先删除所有的id
        tempMenuService.remove(new QueryWrapper<SysTempMenu>().lambda().eq(SysTempMenu::getTempId,sysTemplate.getId()));
        Collection<SysTempMenu> sysTemplates = new ArrayList<>();
        for (Integer menuId : menuIds){
            SysTempMenu sysTempMenu = new SysTempMenu();
            sysTempMenu.setMenuId(menuId.longValue());
            sysTempMenu.setTempId(sysTemplate.getId());
            sysTemplates.add(sysTempMenu);
        }
        tempMenuService.saveBatch(sysTemplates);
    }
}
